package com.lrkj.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.service.TbEmployeeService;
import com.lrkj.back.entity.TbEmployee;
import com.lrkj.back.service.TbAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lrkj.back.entity.TbAdvice;
import cn.com.cbons.mamisay.parent.exception.BusinessException;

@Controller
@RequestMapping("advice")
public class TbAdvcieController extends BaseController {

	@Autowired
	private TbAdviceService adviceService;

	@Autowired
	private TbEmployeeService employeeService;
	
	@RequestMapping("findOne.do")
	public ModelAndView findOne(String id, HttpServletRequest request, HttpServletResponse response) throws BusinessException {
		TbAdvice advice = adviceService.selectByPrimaryKey(id);
		request.setAttribute("advice", advice);
		return new ModelAndView("/system/advice/advice_content");
	}
	
	@RequestMapping("adviceAdd.do")
	public ModelAndView advcieAdd(HttpServletRequest request, HttpServletResponse response) throws BusinessException {
		Map<String, Object> map = new HashMap<>();
		List<TbEmployee> employees = employeeService.findEmployee(map);
		TbEmployee employee = (TbEmployee)session.getAttribute("emp");
		if (employee != null) {
			request.setAttribute("empId",employee.getEmployeeId());
		}
		//
		request.setAttribute("employee",employees);
		return new ModelAndView("/system/advice/advice_add");
	}
	
	@RequestMapping("adviceSave.do")
	@ResponseBody
	public Map<Object,Object> findAdvice(TbAdvice advice, HttpServletRequest request, HttpServletResponse response) throws BusinessException {
		Map<Object, Object> rtnMap = new HashMap<>();
		if (advice != null) {
			TbEmployee employee = (TbEmployee)session.getAttribute("emp");
			if (employee != null) {
				advice.setAdviceEmployee(employee.getEmployeeId());
			}
			advice.setAdviceTime(new Date());
			adviceService.insert(advice);
			rtnMap.put("msg", "提交成功");
			rtnMap.put("status", "true");
		}else {
			rtnMap.put("msg", "提交失败");
			rtnMap.put("status", "false");
		}
		return rtnMap;
	}
}
